package com.z.relam;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;

/**
 * @ProjectName: springboot-08-shiro
 * @Package: realm
 * @ClassName: CustomerMd5Realm
 * @author: Arisapoke
 * @date: 2021/2/2 19:32
 * @Version: 1.0
 */
public class CustomerMd5Realm extends AuthorizingRealm {


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        String primaryPrincipal = (String)principalCollection.getPrimaryPrincipal();
        System.out.println("身份信息: "+primaryPrincipal); //用户名

        //根据身份信息 用户名 获取当前用户的角色信息，以及权限信息
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        //假设 admin,user 是从数据库查到的 角色信息
        simpleAuthorizationInfo.addRole("admin");
        simpleAuthorizationInfo.addRole("user");
//        //假设 ... 是从数据库查到的 权限信息赋值给权限对象
//        simpleAuthorizationInfo.addStringPermission("user:*:01");
        simpleAuthorizationInfo.addStringPermission("prodect:*");//第三个参数为*省略
        return simpleAuthorizationInfo;

    }

    //md5 + salt + hash
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal = (String) authenticationToken.getPrincipal();
        if ("zhn".equals(principal)){
            //参数1 用户名 参数2 带盐的密码 参数3 注册时的随机盐 参数4 realm的名字
            return new SimpleAuthenticationInfo(principal,
                    "3083abb741892baec02974b5a3415e7f",
                    ByteSource.Util.bytes("逼逼莱濑"),
                    this.getName());
        }
        return null;
    }
}
